#include <iostream>
#include "NewtonInterpolation.h"
#include <vector>
#include <cmath>
#include <iomanip>
#include "matplotlibcpp.h"

using namespace std;
namespace plt = matplotlibcpp;

int main()
{
    vector<double> t = {0,6,10,13,17,20,28};
    vector<double> f1 = {6.67,17.3,42.7,37.3,30.1,29.3,28.7};
    vector<double> f2 = {6.67,16.1,18.9,15.0,10.6,9.44,8.89};
    Newton H1(t,f1);
    Newton H2(t,f2);
    Polynomial p1 = H1.polation();
    Polynomial p2 = H2.polation();

    int n = 1000;    //描点画图的点数
    double a = 0 , b = 30.0;  //区间
    double gap = (b - a) / n;
    std::vector<double> x(n+1), y(n+1);

    plt::figure();          //创建一张图
        for (int j = 0; j <= n; j++)
        {
            x[j] = a + j * gap;
            y[j] = p1(x[j]);
        }
    plt::plot(x, y,{{"label", "Sp1"}});

        for (int j = 0; j <= n; j++)
        {
            x[j] = a + j * gap;
            y[j] = p2(x[j]);
        }
    plt::plot(x, y,{{"label", "Sp2"}});

        for (int j = 0; j <= n; j++)
        {
            x[j] = a + j * gap;
            y[j] = 0;
        }
    plt::plot(x, y,"--");
        
    plt::xlim(a, b);
    plt::title("Sample Curve"); // set a title
    plt::legend();                // enable the legend

    plt::save("E.png");

    cout << "E: Plot build successfully" << endl;
    cout << "When t = 43, sp1 = " << setiosflags(ios::fixed) << setprecision(0) << p1(43)
    << ", sp2 = " << setiosflags(ios::fixed) << setprecision(0) << p2(43) << endl;
    cout << "It's wrong obviously.So it can't predict too far.By common sense, sp2 will die but sp1 not." << endl;


}